home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / x+opengl / glxsimple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.3 KB  |  196 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* $Revision: 1.6 $ */
  18. /* compile: cc -o glxsimple glxsimple.c -lGL -lX11 */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <GL/glx.h>        /* this includes the necessary X headers */
  22. #include <GL/gl.h>
  23.  
  24. static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
  25. static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
  26.  
  27. Display        *dpy;
  28. Window          win;
  29. GLfloat         xAngle = 42.0, yAngle = 82.0, zAngle = 112.0;
  30. GLboolean    doubleBuffer = GL_TRUE;
  31.  
  32. void
  33. fatalError(char *message)
  34. {
  35.     fprintf(stderr, "glxsimple: %s\n", message);
  36.     exit(1);
  37. }
  38.  
  39. void
  40. redraw(void)
  41. {
  42.     static GLboolean   displayListInited = GL_FALSE;
  43.  
  44.     if (displayListInited) {
  45.     /* if display list already exists, just execute it */
  46.     glCallList(1);
  47.     } else {
  48.     /* otherwise compile and execute to create the display list */
  49.     glNewList(1, GL_COMPILE_AND_EXECUTE);
  50.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  51.     /* front face */
  52.     glBegin(GL_QUADS);
  53.     glColor3f(0.0, 0.7, 0.1);    /* green */
  54.     glVertex3f(-1.0, 1.0, 1.0);
  55.     glVertex3f(1.0, 1.0, 1.0);
  56.     glVertex3f(1.0, -1.0, 1.0);
  57.     glVertex3f(-1.0, -1.0, 1.0);
  58.     /* back face */
  59.     glColor3f(0.9, 1.0, 0.0);    /* yellow */
  60.     glVertex3f(-1.0, 1.0, -1.0);
  61.     glVertex3f(1.0, 1.0, -1.0);
  62.     glVertex3f(1.0, -1.0, -1.0);
  63.     glVertex3f(-1.0, -1.0, -1.0);
  64.     /* top side face */
  65.     glColor3f(0.2, 0.2, 1.0);    /* blue */
  66.     glVertex3f(-1.0, 1.0, 1.0);
  67.     glVertex3f(1.0, 1.0, 1.0);
  68.     glVertex3f(1.0, 1.0, -1.0);
  69.     glVertex3f(-1.0, 1.0, -1.0);
  70.     /* bottom side face */
  71.     glColor3f(0.7, 0.0, 0.1);    /* red */
  72.     glVertex3f(-1.0, -1.0, 1.0);
  73.     glVertex3f(1.0, -1.0, 1.0);
  74.     glVertex3f(1.0, -1.0, -1.0);
  75.     glVertex3f(-1.0, -1.0, -1.0);
  76.     glEnd();
  77.     glEndList();
  78.     displayListInited = GL_TRUE;
  79.     }
  80.     if(doubleBuffer) glXSwapBuffers(dpy, win); /* buffer swap does implicit glFlush */
  81.        else glFlush(); /* explicit flush for single buffered case */
  82. }
  83.  
  84. void
  85. main(int argc, char **argv)
  86. {
  87.     XVisualInfo    *vi;
  88.     Colormap        cmap;
  89.     XSetWindowAttributes swa;
  90.     GLXContext      cx;
  91.     XEvent          event;
  92.     GLboolean       needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
  93.     int            dummy;
  94.  
  95.     /*** (1) open a connection to the X server ***/
  96.  
  97.     dpy = XOpenDisplay(NULL);
  98.     if (dpy == NULL) fatalError("could not open display");
  99.  
  100.     /*** (2) make sure OpenGL's GLX extension supported ***/
  101.  
  102.     if(!glXQueryExtension(dpy, &dummy, &dummy)) fatalError("X server has no OpenGL GLX extension");
  103.  
  104.     /*** (3) find an appropriate visual ***/
  105.  
  106.     /* find an OpenGL-capable RGB visual with depth buffer */
  107.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
  108.     if (vi == NULL) {
  109.        vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
  110.        if (vi == NULL) fatalError("no RGB visual with depth buffer");
  111.        doubleBuffer = GL_FALSE;
  112.     }
  113.     if(vi->class != TrueColor) fatalError("TrueColor visual required for this program");
  114.  
  115.     /*** (4) create an OpenGL rendering context  ***/
  116.  
  117.     /* create an OpenGL rendering context */
  118.     cx = glXCreateContext(dpy, vi, /* no sharing of display lists */ None,
  119.               /* direct rendering if possible */ GL_TRUE);
  120.     if (cx == NULL) fatalError("could not create rendering context");
  121.  
  122.     /*** (5) create an X window with the selected visual ***/
  123.  
  124.     /* create an X colormap since probably not using default visual */
  125.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
  126.     swa.colormap = cmap;
  127.     swa.border_pixel = 0;
  128.     swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
  129.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300, 0, vi->depth,
  130.                         InputOutput, vi->visual, CWBorderPixel | CWColormap | CWEventMask, &swa);
  131.     XSetStandardProperties(dpy, win, "glxsimple", "glxsimple", None, argv, argc, NULL);
  132.  
  133.     /*** (6) bind the rendering context to the window ***/
  134.  
  135.     glXMakeCurrent(dpy, win, cx);
  136.  
  137.     /*** (7) request the X window to be displayed on the screen ***/
  138.  
  139.     XMapWindow(dpy, win);
  140.  
  141.     /*** (8) configure the OpenGL context for rendering ***/
  142.  
  143.     glEnable(GL_DEPTH_TEST); /* enable depth buffering */
  144.     glDepthFunc(GL_LESS);    /* pedantic, GL_LESS is the default */
  145.     glClearDepth(1.0);       /* pedantic, 1.0 is the default */
  146.     /* frame buffer clears should be to black */
  147.     glClearColor(0.0, 0.0, 0.0, 0.0);
  148.     /* set up projection transform */
  149.     glMatrixMode(GL_PROJECTION);
  150.     glLoadIdentity();
  151.     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  152.     /* establish initial viewport */
  153.     glViewport(0, 0, 300, 300); /* pedantic, full window size is default viewport */
  154.  
  155.     /*** (9) dispatch X events ***/
  156.  
  157.     while (1) {
  158.     do {
  159.         XNextEvent(dpy, &event);
  160.         switch (event.type) {
  161.         case ButtonPress:
  162.         recalcModelView = GL_TRUE;
  163.         switch (event.xbutton.button) {
  164.         case 1: xAngle += 10.0; break;
  165.         case 2: yAngle += 10.0; break;
  166.         case 3: zAngle += 10.0; break;
  167.         }
  168.         break;
  169.         case ConfigureNotify:
  170.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  171.         /* fall through... */
  172.         case Expose:
  173.         needRedraw = GL_TRUE;
  174.         break;
  175.         }
  176.     } while(XPending(dpy)); /* loop to compress events */
  177.     if (recalcModelView) {
  178.         glMatrixMode(GL_MODELVIEW);
  179.         /* reset modelview matrix to the identity matrix */
  180.         glLoadIdentity();
  181.         /* move the camera back three units */
  182.         glTranslatef(0.0, 0.0, -3.0);
  183.         /* rotate by X, Y, and Z angles */
  184.         glRotatef(xAngle, 0.1, 0.0, 0.0);
  185.         glRotatef(yAngle, 0.0, 0.1, 0.0);
  186.         glRotatef(zAngle, 0.0, 0.0, 1.0);
  187.         recalcModelView = GL_FALSE;
  188.         needRedraw = GL_TRUE;
  189.     }
  190.     if (needRedraw) {
  191.         redraw();
  192.         needRedraw = GL_FALSE;
  193.     }
  194.     }
  195. }
  196.